<template>
  <div @touchstart='touchstart' @touchmove='touchmove'>
</div>

  <!-- 头部导航 -->
  <van-nav-bar title="XXX的小店" left-text="返回" left-arrow @click-left="onClickLeft" >
    <template #right>
        <van-icon name="wap-nav" color="black" size="20" @click="showTop = true" />
      </template></van-nav-bar>
<!-- 商品展示  thumb-link="/navbar"-->
<van-swipe-cell v-for="g in goods" :key="g.goodsId">
    <van-card to="goods" :price="(g.price)" :title="(g.goodsName)" :desc="(g.details)" :thumb="getImageUrl(g.picture)"
    @click-thumb="toGoodsList(g.goodsId)" >
      <router-link :to="{ name: 'goods' }"></router-link>
      <template #tags>
        <van-tag plain type="primary">{{ g.brand }}</van-tag>
      </template>
      <template #footer>
        <van-button size="mini" to="message">联系卖家</van-button>
        <van-button size="mini" to="tocomment">查看评价</van-button>
      </template>
    </van-card>
    <!-- 滑动删除 -->
    <template #right>
      <van-button square text="删除" type="danger" class="delete-button" />
    </template>
  </van-swipe-cell>
  <nut-popup position="top" closeable :style="{ height: '20%' }" teleport-disable v-model:visible="showTop" bottom>
    <van-col offset="2">
      <div class="font">快捷入口</div>
    </van-col><br>
    <van-col>
    <nut-row type="flex" justify="space-evenly" class="popup">
      <nut-col :span="4" align="center" @click="toFirstPageView">
        <IconFont name="home" size="30"></IconFont>
        <div>首页</div>
      </nut-col>
      <nut-col :span="4" align="center" @click="toPersonalView">
        <IconFont name="people" size="30"></IconFont>
        <div>个人主页</div>
      </nut-col>
    </nut-row></van-col>
  </nut-popup>
<!-- 对话框 -->
<div class="container">
    <div class="main">
      <div class="box">
        <div class="title">
<img src="" alt class="logo" />  
        </div>
        <div id="content" class="content">
          <div v-for="(item,index) in info" :key="index">
<!-- 商家发送消息 -->
            <div class="info_r info_default" v-if="item.type == 'leftinfo'">
              <span class="circle circle_r">
                <img src="../assets/image/avg.jpg" class="pic_r" />
              </span>
              <div class="con_r con_text">
                <div>{{item.content}}</div>
                <div v-for="(item2,index) in item.question" :key="index">
                  <div class="con_que" @click="clickRobot(item2.content,item2.id)">
                    <div class="czkj-question-msg">
                      {{item2.index}}
                      {{item2.content}}
                    </div>
                  </div>
                </div>
              </div>
              <div class="time_r">{{item.time}}</div>
            </div>
 <!-- 用户发送消息 -->
            <div class="info_l" v-if="item.type == 'rightinfo'">
              <div class="con_r con_text">
                <span class="con_l">{{item.content}}</span>
                <span class="circle circle_l">
                  <img src="../assets/image/avg2.png" class="pic_l" />
                </span>
              </div>
              <div class="time_l">{{item.time}}</div>
            </div>
          </div>
        </div>
 <!-- 输入框，发送按钮 -->
        <div class="setproblem">
          <textarea
            placeholder="请输入您的问题..."
            style="height: 50px;width: 100%;resize:none;padding-right:50px;outline: none;border-color:#ccc;border-radius:5px;"
            id="text"
            v-model="customerText"
            @keyup.enter="sentMsg()"
          ></textarea>
          <button @click="sentMsg()" class="setproblems">
            <span style="vertical-align: 3px;">发 送</span>
          </button>
        </div>
      </div>
    </div>
  </div>
</template>  
<script>
import { onMounted } from 'vue';
import { reactive } from 'vue';
import { useRouter,useRoute } from 'vue-router'; //路由器
import { default as http, getImageUrl } from '@/utils/request.js';
import { ref } from 'vue';

  export default {
    setup() {
    const onClickLeft = () => history.back();
    const router = useRouter(); //获取路由器对象
    const toGoodsView=function(){
      router.push("goods");
    }
    //商品 详情页
const toGoodsList=function(goodsId){
  console.log("商品ID:",goodsId);
  router.push({ name: 'goods', params: { goodsId: goodsId } });
}

const getGoods = function () {
  http.get("/user/info").then((response) => {
   console.log("获取个人信息 = ", response);
   console.log("id:", response.data.data.id);

 })
};
//显示弹出层
const showTop = ref(false);
//跳转到个人页面
function toPersonalView() {
  router.push("/personal");
}
//跳转到首页
function toFirstPageView() {
  router.push("/");
}
// const route = useRoute();   //获取路由对象

// console.log('router 路由器 = ', router);
// console.log('route 路由 = ', route);
// console.log('messagelist.id = ', route.params.id); 
    return {
      onClickLeft,
      toGoodsView,
      toGoodsList,
      getGoods,
      showTop,
      toPersonalView,
      toFirstPageView,
    };
    
  },
    name: "onlineCustomer",
    components: {},
    computed: {},
    data() {
      // 机器自动发送
      return {
        customerText: "",
        info: [
          {
            type: "leftinfo",
            time: this.getTodayTime(),
            name: "robot",
            content:
              "尊敬的客户您好，欢迎光顾本店",
            question: [],
          },
          
        ],
        // 用发送想问的问题
        robotQuestion: [
          { id: 1, content: "满168减30？", index: 1 },
          { id: 2, content: "发货吗", index: 2 },
          { id: 3, content: "发什么快递", index: 3 },
          {
            id: 4,content: "尺码是国际标准吗？",index: 4,
          },
          
        ],
// 机器自动回答
        robotAnswer: [
          {
            id: 1,content:"是的呢亲",index: 1,
          },
          { id: 2, content: "现在下单就发货呢亲", index: 2 },
          { id: 3, content: "邮政呢亲", index: 3 },
          {
            id: 4,content: "是的呢亲",index: 4,
          },
        ],
      };
    },
    created() {
      this.showTimer();
    },
    watch: {},
    methods: {
      // 用户发送消息
      sentMsg() {
        clearTimeout(this.timer);
        this.showTimer();
        let text = this.customerText.trim();
        if (text != "") {
          var obj = {
            type: "rightinfo",
            time: this.getTodayTime(),
            content: text,
          };
          this.info.push(obj);
          this.appendRobotMsg(this.customerText);
          this.customerText = "";
          this.$nextTick(() => {
            var contentHeight = document.getElementById("content");
            contentHeight.scrollTop = contentHeight.scrollHeight;
          }
          );
        }
      },
      // 机器人回答消息
      appendRobotMsg(text) {
        clearTimeout(this.timer);
        this.showTimer();
        text = text.trim();
        let answerText = "";
        let flag;
        for (let i = 0; i < this.robotAnswer.length; i++) {
          if (this.robotAnswer[i].content.indexOf(text) != -1) {
            flag = true;
            answerText = this.robotAnswer[i].content;
            break;
          }
        }
        if (flag) {
          let obj = {
            type: "leftinfo",
            time: this.getTodayTime(),
            name: "我",
            content: answerText,
            question: [],

          };
          this.info.push(obj);
        } 
        else {
          answerText = "您可能想问：";
          let obj = {
            type: "leftinfo",
            time: this.getTodayTime(),
            name: "robot",
            content: answerText,
            question: this.robotQuestion,
          };
          this.info.push(obj);
        }
        this.$nextTick(() => {
          var contentHeight = document.getElementById("content");
          contentHeight.scrollTop = contentHeight.scrollHeight;
        });
      },
      sentMsgById(val, id) {
        clearTimeout(this.timer);
        this.showTimer();
 
        let robotById = this.robotAnswer.filter((item) => {
          return item.id == id;
        });
        // 点击连接后自动回复的答案
        let obj_l = {
          type: "leftinfo",
          time: this.getTodayTime(),
          name: "robot",
          content: robotById[0].content,
          question: [],
        };
        // 我发出的消息
        let obj_r = {
          type: "rightinfo",
          time: this.getTodayTime(),
          name: "robot",
          content: val,
          question: [],
        };
        this.info.push(obj_r);
        this.info.push(obj_l);
        this.$nextTick(() => {
          var contentHeight = document.getElementById("content");
          contentHeight.scrollTop = contentHeight.scrollHeight;
        });
      },
      // 点击机器人的单个问题
      clickRobot(val, id) {
        this.sentMsgById(val, id);
      },
      // 结束语
      endMsg() {
        let happyEnding = {
          type: "leftinfo",
          time: this.getTodayTime(),
          content: "退下吧",
          question: [],
        };
        this.info.push(happyEnding);
        this.$nextTick(() => {
          var contentHeight = document.getElementById("content");
          contentHeight.scrollTop = contentHeight.scrollHeight;
        });
 
      },
      showTimer() {
        this.timer = setTimeout(this.endMsg, 1000*60);
      },
      getTodayTime() {
        // 获取当前时间
        var day = new Date();
        let seconds = day.getSeconds();
        if (seconds < 10) {
          seconds = "0" + seconds;
        } else {
          seconds = seconds;
        }
        let minutes = day.getMinutes();
        if (minutes < 10) {
          minutes = "0" + minutes;
        } else {
          minutes = minutes;
        }
        let time =
          day.getFullYear() +
          "-" +
          (day.getMonth() + 1) +
          "-" +
          day.getDate() +
          " " +
          day.getHours() +
          ":" +
          minutes +
          ":" +
          seconds;
        return time;
      },
    },

    onMounted() {getGoods(); },
    props: {},
    destroyed() {},
  };
</script>
<style lang="scss">
  .main {
    width: 100%;
    // height: 100vh;
    background: linear-gradient(
        180deg,
        rgb(212, 183, 149) 0%,
        rgba(74, 131, 194, 1) 100%
    );
    overflow: hidden;
    .box {
      width: 100%;
      // /* width: 680px; *
      height: 780px;
      background-color: #fafafa;
      position: relative;
      padding: 1.25rem;
      #content {
        height: 650px;
        overflow-y: scroll;
        font-size: 14px;
        width: 100%;
        .circle {
          display: inline-block;
          width: 34px;
          height: 34px;
          border-radius: 50%;
          background-color: #dee1e5;
        }
        
        .con_text {
          color: #333;
          margin-bottom: 5px;
        }
        .con_que {
          color: #eadf10;
          height: 30px;
          line-height: 30px;
          cursor: pointer;
        }
        .info_r {
          position: relative;
          .circle_r {
            position: absolute;
            left: 0%;
          }
          .pic_r {
            display: inline-block;
            border-radius: 50%;
            overflow: hidden;
            width: 34px;
            height: 34px;
            margin: 1px;
          }
          
          .con_r {
            display: inline-block;
            /* max-width: 253px; */
            width: 55%;
            min-height: 55px;
            /* min-height: 20px; */
            background-color: rgb(236, 235, 187);
            border-radius: 6px;
            padding: 10px;
            margin-left: 40px;
          }
          .time_r {
            margin-left: 45px;
            color: #999999;
            font-size: 12px;
          }
        }
       
        .info_l {
          text-align: right;
          // margin-right: 20px;
          color: #ffffff;
          color: #c0c531;
          margin-top: 10px;
 
          // .circle_l {
          //   // vertical-align: -10px;
          // }
          .pic_l {
            width: 13px;
            height: 17px;
            margin: 8px 10px;
          }
          .time_l {
            margin-right: 45px;
            color: #999999;
            font-size: 12px;
            margin-top: 5px;
          }
          .con_l {
            display: inline-block;
            width: 220px;
            min-height: 51px;
            background-color: rgb(250, 210, 33);
            border-radius: 6px;
            padding: 10px;
            text-align: left;
            color: #1a1a1a;
            margin-right: 5px;
          }
          .pic_l {
            display: inline-block;
            border-radius: 50%;
            overflow: hidden;
            width: 35px;
            height: 35px;
            margin: 1px;
          }
        }
        #question {
          cursor: pointer;
        }
      }
    }
  }
  .setproblem {
    width: 100%;
    height: 70px;
    background-color: #ffffff;
    position: relative;
    margin-top: 2.2rem;
    
  }
  .setproblem textarea {
    color: #070707;
    padding: 10px;
    box-sizing: border-box;
  }
  .setproblem button {
    width: 5rem;
    height: 2.5rem;
    line-height: 2.5rem;
    background: rgb(255, 226, 8);
    opacity: 1;
    border-radius: 4px;
    font-size: 10px;
    color: #171616;
    position: absolute;
    right: 5%;
    top: 10%;
    cursor: pointer;
    border: none;
  }
 
  .czkj-item-title {
    line-height: 25px;
    border-bottom: 1px solid #ccc;
    padding-bottom: 5px;
    margin-bottom: 5px;
  }
 
  .czkj-item-question {
    cursor: pointer;
    display: block;
    padding: 8px;
    position: relative;
    border-bottom: 1px dashed #ccc;
    line-height: 20px;
    min-height: 20px;
    overflow: hidden;
  }
 
  .czkj-question-msg {
    float: left;
    font-size: 14px;
    color: #3163C5;
  }
  </style>
